"""
BIF(built-in functions) 内建函数。
"""


class A:
    pass


class B(A):
    pass


class C:
    def __init__(self, name='abc'):
        self.name = name


# 判断前者是否是后者的子类
print(issubclass(B, A))  # True
print(issubclass(B, B))  # True
print(issubclass(B, (B, A, object)))  # True
print(issubclass(B, C))  # False

# 判断前者是否是后者的对象
b = B()
print(isinstance(b, B))  # True
print(isinstance(b, A))  # True
print(isinstance(b, (B, A, C)))  # True
print(isinstance(b, C))  # False

# 判断对象（前者）是否拥有此属性（后者）,属性要用引号
c = C()
print(hasattr(b, 'name'))  # False
print(hasattr(c, 'name'))  # True
# 获取对象的属性值
print(getattr(c, 'name'))  # abc
print(getattr(c, 'age', '属性不存在'))  # 属性不存在
# 设置属性值
setattr(c, 'name', 'dylan')
print(getattr(c, 'name'))  # dylan

if hasattr(c, 'name'):
    print('删除属性')
    # 删除属性
    delattr(c, 'name')
else:
    print('属性不存在')

print(hasattr(c, 'name'))  # False


# property() 函数的作用是在新式类中返回属性值
class D:
    def __init__(self, name):
        self.name = name

    def getName(self):
        return self.name

    def setName(self, value):
        self.name = value

    def delName(self):
        del self.name

    pro = property(getName, setName, delName)


d = D('dylan')
# 获取值
print(d.pro)  # dylan
# 设置值
d.pro = 'Hello'
print(d.pro)  # Hello
# 删除属性值
del d.pro
print(hasattr(d, 'name'))  # False
# AttributeError: 'D' object has no attribute 'name'
# print(d.pro)
